<?php

namespace Plugins\AdminBasic\Http\Admin\Controller;


use App\Exception\ApiMessageException;
use App\Middlewares\Admin\AuthenticateMiddleware;
use App\Middlewares\Admin\PermissionsMiddleware;
use App\Util\Helper;
use App\Util\Response;
use Exception;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\View\Render;
use Plugins\AdminBasic\Http\Admin\Service\ApiMakeService;
use Plugins\AdminBasic\Http\Admin\Service\DevAuxService;
use Plugins\AdminBasic\Http\Admin\Service\MakeCURDService;
use Plugins\AdminBasic\Http\Admin\Service\UpdateCURDService;
use Psr\Http\Message\ResponseInterface;
use Sc\Util\ScTool;

/**
 * 开发辅助
 *
 * Class DevAxuController
 */
#[Controller(prefix: 'admin/plugins/admin-basic/dev-aux')]
#[Middlewares([AuthenticateMiddleware::class, PermissionsMiddleware::class])]
class DevAuxController
{
    /**
     * 基础文件创建
     *
     * @param Render $render
     *
     * @return ResponseInterface
     */
    #[GetMapping(path: 'file-make')]
    public function baseFileMake(Render $render): ResponseInterface
    {
        $pluginsDir = BASE_PATH . '/plugins';
        return $render->render("plugins.AdminBasic.Admin.Dev.file-make", [
            'plugins' => is_dir($pluginsDir) ? array_map(fn($dir) => ['name' => $dir], ScTool::dir($pluginsDir)->getDirs()) : []
        ]);
    }

    /**
     * 获取字段详情
     *
     * @return ResponseInterface
     */
    #[GetMapping(path: 'get-table-fields')]
    public function getTableFields(): ResponseInterface
    {
        $table = Helper::request()->query('table');

        $fields = DevAuxService::aop()->getFields($table);

        return Response::json()->success($fields);
    }

    /**
     * 获取所有表
     *
     * @return ResponseInterface
     */
    #[GetMapping(path: 'get-all-tables')]
    public function getAllTables(): ResponseInterface
    {
        $tableInfo = DevAuxService::aop()->getAllTableInfo();

        return Response::json()->success($tableInfo);
    }


    /**
     * 创建CURD
     *
     * @return ResponseInterface
     */
    #[PostMapping(path: 'make-curd')]
    public function makeCURD(): ResponseInterface
    {
        $res = (new MakeCURDService(Helper::request()->post()))->make();

        return Response::json()->success($res);
    }

    /**
     * 检查已存在的文件
     *
     * @return ResponseInterface
     * @throws Exception
     */
    #[GetMapping(path: 'curd-files-check')]
    public function checkCURDFiles(): ResponseInterface
    {
        $res = MakeCURDService::check(
            Helper::request()->query('table'),
            Helper::request()->query('dir'),
            Helper::request()->query('plugins'),
        );

        return Response::json()->success($res);
    }

    /**
     * 清除CURD文件
     *
     * @return ResponseInterface
     */
    #[PostMapping(path: 'curd-clear')]
    public function CURDClear(): ResponseInterface
    {
        MakeCURDService::clear(Helper::request()->post('files'));

        return Response::json()->success();
    }

    /**
     * 获取已有的文件夹
     *
     * @return ResponseInterface
     */
    #[GetMapping(path: 'get-make-dirs')]
    public function getMakeDirs(): ResponseInterface
    {
        return Response::json()->success(
            MakeCURDService::getCurrentDirs(Helper::request()->query('plugins'))
        );
    }

    /**
     * 更新文件
     *
     * @param Render $render
     *
     * @return ResponseInterface
     */
    #[GetMapping(path: 'file-update')]
    public function baseFileUpdate(Render $render): ResponseInterface
    {
        return $render->render('plugins.AdminBasic.Admin.Dev.file-update', [
            'plugins' => ScTool::dir(BASE_PATH . '/plugins')->getDirs()
        ]);
    }


    /**
     * 检测更新的文件
     *
     * @return ResponseInterface
     */
    #[GetMapping(path: 'update-file-check')]
    public function updateFileCheck(): ResponseInterface
    {
        $result = (new UpdateCURDService(
            (string)Helper::request()->query('table'),
            (string)Helper::request()->query('table_comment'),
            (string)Helper::request()->query('dir'),
            (string)Helper::request()->query('plugins'),
        ))->check();

        return Response::json()->success($result);
    }

    /**
     * 执行文件更新
     *
     * @return ResponseInterface
     */
    #[PostMapping(path: 'update-file-exec')]
    public function updateFileExec(): ResponseInterface
    {
        $res = (new UpdateCURDService(
            (string)Helper::request()->post('table'),
            (string)Helper::request()->post('table_comment'),
            (string)Helper::request()->post('dir'),
            (string)Helper::request()->post('plugins'),
        ))->exec();

        return Response::json()->success('', $res);
    }

    /**
     * API创建
     *
     * @param Render $render
     *
     * @return ResponseInterface
     */
    #[GetMapping('api-make')]
    public function apiMake(Render $render): ResponseInterface
    {
        return $render->render("plugins.AdminBasic.Admin.Dev.api-make");
    }

    /**
     * api模块的文件分析
     *
     * @return ResponseInterface
     * @throws Exception
     */
    #[GetMapping('api-module-file')]
    public function apiModuleFile(): ResponseInterface
    {
        $apiModuleFileQuery = ApiMakeService::aop()->ApiModuleFileQuery(Helper::request()->query('module'));
        return Response::json()->success($apiModuleFileQuery);
    }

    /**
     * 获取文件内容
     *
     * @return ResponseInterface
     * @throws ApiMessageException
     */
    #[PostMapping('get-file-content')]
    public function getFileContent(): ResponseInterface
    {
        $apiModuleFileQuery = ApiMakeService::aop()->getFileContent(
            Helper::request()->post('path'),
            Helper::request()->post('update')
        );
        return Response::json()->success($apiModuleFileQuery);
    }
}